package com.podhradie.net.themperature.tools;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import com.podhradie.net.themperature.gui.StartWindow;
import com.podhradie.net.themperature.tools.Context.LogCategory;

public class PropertyHelper {
	private static String FILE_NAME = "config.properties";

	public static void writePropertyFile(Properties p_prop) throws Exception {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(FILE_NAME);
			p_prop.store(fos, "Properties file updated");
		} catch (Exception e) {
			System.err.println("Error in writing Property file.");
			throw new Exception();
		}
	}

	public static Properties readPropertyFile() throws Exception {
		FileInputStream fis = null;
		Properties prop = null;
		try {
			fis = new FileInputStream(FILE_NAME);
			if (fis != null) {
				prop = new Properties();
				prop.load(fis);
			} else {
				// Error in reading Property file

			}
		} catch (Exception e) {
			Context.get().printLine(LogCategory.PROPERTY, "Error in reading Property file.");
			System.err.println("Error in reading Property file. Exception Message = "+ e.getMessage());
			prop = null;
		}

		finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (Exception ie) {
					System.err.println("Error in closing the File Input Stream; Exception Message = "+ie.getMessage());
				}
			}
		}
		return prop;
	}

}
